import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:h_flutter_119/base/pageWidget/CommonStatelessWidget.dart';
import 'package:h_flutter_119/page/home/HomePage.dart';
import 'package:h_flutter_119/route/route_utils.dart';

import '../model/home_eye_itemlist_entity.dart';

class ItemHomeWidget extends CommonStatelessWidget {

  EyeItemList itemData;
  SwiperController _controller;

  ItemHomeWidget(this._controller, this.itemData, {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var type = itemData.type;
    if(type == 'banner2'){
      return _createSwipe();
    }else if(type == 'video'){
      return GestureDetector(
        onTap: () {
          var playUrl = itemData.data?.playUrl;
          var coverUrl = itemData.data?.cover?.feed;
          var videoId = itemData.data?.id;
          var map = <String, String>{};
          map["playUrl"] = playUrl ?? "";
          map["coverUrl"] = coverUrl ?? "";
          map["videoId"] = "$videoId";
          goVideoDetail(context, map);
        },
        child: _createItem(context,itemData),
      );
    }
    return const SizedBox(height: 0,);
  }

  Widget _createSwipe() {
    var bannerUrl = ["https://gd-hbimg.huaban.com/cd4d1386b496089e6156749233836583a78f3afe5eabb-HfbLqh_fw658webp"];
    return SizedBox(
      height: 400.w,
      child: Swiper(
        controller: _controller,
        autoplay: true,
        autoplayDelay: 3000,
        itemCount: bannerUrl.length,
        pagination: SwiperPagination(
          builder: DotSwiperPaginationBuilder(
            color:Colors.grey.shade900,
            activeColor: Colors.white,
            size:12.w ,
            activeSize: 12.w
          )
        ),
        itemBuilder: (context,index){
          return Image.network(bannerUrl[index],height: 400.w,fit: BoxFit.fitWidth,);
        },
      ),
    );
  }

  Widget _createItem(BuildContext context,EyeItemList itemData) {
    return Card(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(20.w))
      ),
      child: Column(
        children: [
          Stack(
            children: [
              Hero(tag: itemData.data?.id.toString() ?? "", child: Image.network(itemData.data?.cover?.feed??"",height: 400.w,fit: BoxFit.cover,width: double.infinity,)),
              Positioned(
                  top: 0,
                  left: 0,
                  child: Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.only(left: 25.w,right: 33.w),
                    height: 42.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(5.w),
                        bottomRight: Radius.circular(42.w)
                      )
                    ),
                    child: Text(itemData.data?.category??"",style: TextStyle(color: Colors.white,fontSize: 24.sp),),
                  ))
            ],
          ),
          SizedBox(
            height: 150.w,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(width: 10.w,),
                GestureDetector(
                  onTap: () {

                  },
                  child: GestureDetector(
                    child: ClipOval(
                      child: Image.network(itemData.data?.author?.icon??"",width: 80.w,height: 80.w),
                    ),
                    onTap: ()=> goPhotoPreview(context, itemData.data?.author?.icon??""),
                  ),
                ),
                SizedBox(width: 8.w,),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: 500.w,
                      child: Text(
                        itemData.data?.author?.name??"",
                        style: TextStyle(
                            color: Colors.black38,
                            fontWeight: FontWeight.bold,
                            fontSize: 30.sp,
                            overflow: TextOverflow.ellipsis
                        ),
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                    ),
                    SizedBox(height: 15.h,),
                    SizedBox(
                      width: 500.w,
                      child: Text(
                        itemData.data?.author?.description??"",
                        style: TextStyle(
                          color: Colors.black38,
                          fontSize: 27.sp,
                          overflow: TextOverflow.ellipsis
                        ),
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

}
